package com.awaken;

import com.proc.AlarmProcessor;
import com.proc.Logger;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: neveragny
 * Date: 03.04.2010
 */
public class Controller {

    private AlarmList alarmList;
    private Missed missed;
    private String title;
    private String type;
    private String occurrence;
    private Date date = new Date();
    private String time;
    private String note;
    private AlarmOne tmpAlarmOne;
    private String status;
    private boolean isProcessorRunned = false;
    private Properties properties;
    
    public AlarmOne getTmpAlarmOne() {
        return tmpAlarmOne;
    }

    public Controller() {
        ExternalContext exContext = FacesContext.getCurrentInstance().getExternalContext();
        Map sessionMap = exContext.getSessionMap();
        alarmList = (AlarmList)sessionMap.get("alarmList");
        missed = (Missed)sessionMap.get("missed");
        this.initProperties();

    }

    private void initProperties(){
        properties = new Properties();
        try{
            properties.load(this.getClass().getResourceAsStream("controller.properties"));
            Logger.log("properties were successfully loaded");
        }
        catch (FileNotFoundException e) {
            Logger.log("property file not found");
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }


    public String getOccurrence() {
        return occurrence;
    }

    public void setOccurrence(String occurrence) {
        this.occurrence = occurrence;
    }


    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }


    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getHeader(){
        return properties.getProperty("header");
    }

    //add new Alarm into AlarmList, holder for alarms
    public String save(){
        AlarmOne alarmOne = new AlarmOne();
        {
            alarmOne.setDate(this.setDate(this.time));
            alarmOne.setNote(this.note);
            alarmOne.setOccurrence(this.occurrence);
            alarmOne.setTitle(this.title);
            alarmOne.setType(this.type);
            alarmOne.setStatus(this.status);
            this.setDate(this.time); // get back simple Calendar instance
        }
        alarmList.addAlarm(alarmOne);
        if(alarmList.getLength() > 0 && !(isProcessorRunned)){
            this.alarmProcessor();
        }
        this.clear();
        return "success";
    }

    public void clear() {
        this.title = null;
        this.note = null;
        this.occurrence = null;
        this.time = null;
        this.type = null;
    }

    public String select(){
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParams = context.getExternalContext().getRequestParameterMap();
        String selectedAlarm = (String) requestParams.get("alarm");
        tmpAlarmOne = alarmList.getAlarmByTitle(selectedAlarm);
        return "success";
    }

    public String clearNotification(){
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestParams = context.getExternalContext().getRequestParameterMap();
        String selectedNotificationTitle = (String) requestParams.get("miss");
        System.out.println(selectedNotificationTitle + " has been founded");
        try{
            missed.deletedByTitle(selectedNotificationTitle);
        }catch (java.util.ConcurrentModificationException exc){
            exc.printStackTrace();
        }
        return "success";
    }

    private void alarmProcessor(){
        AlarmProcessor alarmProcessor = new AlarmProcessor(alarmList, missed);
        Thread thread = new Thread(alarmProcessor);
        thread.start();
        isProcessorRunned = true;
    }

    private Calendar setDate(String time){
        int hour = Integer.parseInt(time.substring(0,2));
        int min =  Integer.parseInt(time.substring(3,5));
        Calendar calendar = GregorianCalendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, min);
        SimpleDateFormat sFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
        Logger.log(sFormat.format(calendar.getTime()));
        return calendar;
    }

    

    
}
